package com.hspedu.list_;



import java.util.*;

/**
 * @Author Stringzhua
 * @Date 2024/3/10 21:18
 * description:
 */
@SuppressWarnings("all")
public class ListExercise02 {
    public static void main(String[] args) {
//        List list = new ArrayList();
//        List list = new Vector();
        List list = new LinkedList();
        list.add(new Book("我的世界", 16.96, "Microsoft"));
        list.add(new Book("滑雪大冒险", 6.63, "EA"));
        list.add(new Book("道德与法制5", 54.69, "R星"));
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println("book = " + next);
        }

        //冒泡排序
        sort(list);
        System.out.println("=====排序后=====");
        //增强for循环
        for (Object o :list) {
            System.out.println("list = " + o);
        }

    }

    //静态方法排序按价格从低到高排序
    public static void sort(List list) {
        int listSize = list.size();
        for (int i = 0; i < listSize - 1; i++) {
            for (int j = 0; j < listSize - 1 - i; j++) {
                //取出对象 Book
                Book book1 = (Book) list.get(j);
                Book book2 = (Book) list.get(j + 1);
                if (book1.getPrice() > book2.getPrice()) {//交换
                    //价格从小到大排序
                    list.set(j, book2);
                    list.set(j + 1, book1);
                }
            }
        }
    }
}


